var express = require('express');
var router = express.Router();
var request = require('request');
var reqUtil = require('../lib/req');
var LINK = require('../api/link');

router.get('/', function(req, res) {
    reqUtil.getData(LINK.REQ_GET_INDEX, function(code, data) {
        console.log(req.session.user);
        if (!code) {
            if (req.session) {
                if (req.session.user) {
                    data.user = true;
                }
            }
            data.nav = true;
            res.render('index', data);
        } else {
            res.redirect('/');
        }
    });
});

router.get('/login', function(req, res) {
    //var link = req.body.link || '/';
    res.render('login', { link: '/' });
});

router.get('/register', function(req, res) {
    reqUtil.getData(LINK.REQ_GET_CITY, function(code, data) {
        if (!code) {
            var obj = {
                _city: JSON.stringify(data)
            };
            res.render('register', obj);
        } else {
            res.redirect('/');
        }
    });
});

router.post('/register', function(req, res) {
    var user_detail = {};
    user_detail.user_nickname = req.body.nickname || null;
    user_detail.user_phone = req.body.mobile || null;
    user_detail.am_id_1 = req.body.province || null;
    user_detail.am_id_2 = req.body.city || null;
    user_detail.am_id_3 = req.body.zone || null;
    user_detail.sm_id = req.body.school || null;
    user_detail.class_id = req.body.cls || null;
    reqUtil.postData(LINK.REQ_POST_USERNAME_CHECK, {
        user_main: { user_name: req.body.username }
    }, function(code, data) {
        if (!code) {
            reqUtil.postData(LINK.REQ_POST_USER_REGISTER, {
                user_main: { user_name: req.body.username, user_password: req.body.password },
                user_detail: user_detail,
                sms_main: { sms_id: req.body.sms, sms_code: req.body.code }
            }, function(code, data) {
                if (!code) {
                    res.json({ code: '0', result: data });
                } else {
                    res.json({ code: code, result: data });
                }
            });
        } else {
            res.json({ code: code, result: data });
        }
    });

});

router.get('/logout', function(req, res) {
    if (req.session) {
        req.session.uid = null;
        req.session.user = null;
    }
    res.redirect('/');
});

router.post('/login', function(req, res) {
    var username = req.body.username;
    var password = req.body.password;
    var link = req.body.link || '/';
    console.log(username, password, link);
    console.log(username, password, link);

    reqUtil.postData(LINK.REQ_POST_USER_LOGIN, { user_name: username, user_password: password }, function(code, data) {
        if (!code) {
            console.log(data);
            req.session.uid = data.user.token;
            req.session.user = data.user;
            res.json({ code: '0', result: data, link: link, });
        } else {
            res.json({ code: code, result: data, link: link, });
        }
    });
});

router.get('/teacher', function(req, res) {
    var page = parseInt(req.query.page) || 1;
    var url = LINK.REQ_GET_TEACHER_LIST;

    reqUtil.getData(url, function(code, data) {
        if (!code) {
            if (req.session) {
                if (req.session.user) {
                    data.user = true;
                }
            }
            res.render('about/teacherList', {
                nav: true,
                user: data.user,
                list: data.page_data,
                "page_now": data.page_now,
                "total_page": data.total_page,
                "total_rows": data.total_rows,
                url: req.baseUrl + req.path
            });
        } else {
            res.redirect('/');
        }
    });
});

router.get('/teacher/:id', function(req, res) {

    var url = LINK.REQ_GET_TEACHER_INFO;
    var id = req.params.id;

    reqUtil.getData(url + id, function(code, data) {
        if (!code) {
            var user = false;
            if (req.session) {
                if (req.session.user) {
                    user = true;
                }
            };
            res.render('about/teacher', {
                course: data.clist,
                teacher: data.teacher,
                user: user
            });
        } else {
            res.redirect('/');
        }
    });

});

router.get('/notice', function(req, res) {
    var url = LINK.REQ_GET_NOTICE_LIST;
    var page = parseInt(req.query.page) || 1;


    reqUtil.getData(url, function(code, data) {
        if (!code) {
            var user = false;
            if (req.session) {
                if (req.session.user) {
                    user = true;
                }
            };
            res.render('about/noticeList', {
                nav: true,
                user: user,
                list: data.page_data,
                "page_now": data.page_now,
                "total_page": data.total_page,
                "total_rows": data.total_rows,
                url: req.baseUrl + req.path
            });
        } else {
            res.redirect('/');
        }
    });
});

router.get('/notice/:id', function(req, res) {
    var id = req.params.id;
    var url = LINK.REQ_GET_NOTICE_INFO;
    reqUtil.getData(url + id, function(code, data) {
        if (!code) {
            if (req.session) {
                if (req.session.user) {
                    data.user = true;
                }
            };
            res.render('about/notice', data);
        } else {
            res.redirect('/');
        }
    });
});

router.get('/help', function(req, res) {
    var url = LINK.REQ_GET_HELP;
    var item = parseInt(req.query.item);
    if (item) {
        url += '&id=' + item;
    }
    reqUtil.getData(url, function(code, data) {
        if (!code) {
            var user = false;
            if (req.session) {
                if (req.session.user) {
                    user = true;
                }
            };
            var list = data.list;
            console.log(list);
            if (!item) {
                if (list) {
                    for (var i in list) {
                        if (list[i].level === '2') {
                            console.log(list[i]);
                            item = list[i].id;
                            break;
                        }
                    }
                }
            }
            res.render('about/help', { content: data.content, list: list, active: item, nav: true, user: user });
        } else {
            res.redirect('/');
        }
    });

});

router.get('/about', function(req, res) {
    var url = LINK.REQ_GET_ABOUT;
    var item = parseInt(req.query.item);
    if (item) {
        url += '&id=' + item;
    }
    reqUtil.getData(url, function(code, data) {
        if (!code) {
            var user = false;
            if (req.session) {
                if (req.session.user) {
                    user = true;
                }
            };
            var list = data.list;
            if (!item) {
                if (list) {
                    if (list.length) {
                        item = list[0].id;
                    };
                }
            }
            res.render('about', { content: data.content, list: list, active: item, nav: true, user: user });
        } else {
            res.redirect('/');
        }
    });
});

router.get('/feedback', function(req, res) {

    res.render('about/feedback', { nav: false });
});

module.exports = router;